package manegement;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import dao.DBFacade;

import bean.CommentBean;

public class CommentManager {
	
	private List<CommentBean> comments;
	
	public CommentManager(String postId) {
		comments = DBFacade.getInstance().getCommentsByPost(postId);
	}

	//Add Methods
	public String addComment(String owner, String content, String login){
		String id = getNewId();
		comments.add(new CommentBean(id, content, login, owner));
		return id;
	}
	
	public String addSubComment(String owner, String content, String login) throws Exception{
		if(getCommentFromId(owner) == null)
			throw new Exception("Invalid comment id.");
		String id = getNewId();
		comments.add(new CommentBean(id, content, login, owner));
		return id;
	}
	
	//Access Methods
	
	public String getCommentContent(String id) throws Exception{
		CommentBean comment = getCommentFromId(id);
		if(comment == null)
			throw new Exception("Invalid comment id");
		return comment.getComment();
	}
	
	public String getAuthor(String id) throws Exception{
		CommentBean comment = getCommentFromId(id);
		if(comment == null)
			throw new Exception("Invalid comment id");
		return comment.getLogin();	
	}
	
	/**
	 * Retrieves the number of comments of post
	 * @return number of comments
	 */
	public int getNumberOfComments(String id) {
		int numberOfComments = 0;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();)
			if(it.next().getOwner().equals(id))
				numberOfComments++;
			
		return numberOfComments;
	}
	
	/**
	 * Retrieves the id of comment of the index passed as parameter
	 * @param index
	 * @return commentId
	 */
	public String getComment(String id, int index) {
		List<CommentBean> commentsFromId = new ArrayList<CommentBean>();
		CommentBean comment;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			comment = it.next();
			if(it.next().getOwner().equals(id))
				commentsFromId.add(comment);
		}

		return commentsFromId.get(index).getId();
	}
	
	public int getNumberOfCommentsByLogin(String id, String login) {
		int numberOfComments = 0;
		CommentBean comment;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			comment = it.next();
			if(comment.getOwner().equals(id) && comment.getLogin().equals(login))
				numberOfComments++;
		}
			
		return numberOfComments;
	}
	
	public String getSubComment(String commentId, int index) throws Exception{
		if( getCommentFromId(commentId) == null)
			throw new Exception("Invalid comment id");
		List<CommentBean> commentsFromId = new ArrayList<CommentBean>();
		CommentBean comment;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			comment = it.next();
			if(it.next().getOwner().equals(commentId))
				commentsFromId.add(comment);
		}

		return commentsFromId.get(index).getId();
	}

	public int getNumberOfSubComments(String commentId) throws Exception{
		if( getCommentFromId(commentId) == null)
			throw new Exception("Invalid comment id");
		int numberOfComments = 0;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			if(it.next().getOwner().equals(commentId))
				numberOfComments++;
		}

		return numberOfComments;
	}

	public int getNumberOfAllSubComments(String commentId) {
		int numberOfComments = 0;
		CommentBean comment;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			comment = it.next();
			if(comment.getOwner().equals(commentId))
				numberOfComments += getNumberOfAllSubComments(comment.getId()) + 1;
		}
		return numberOfComments;
	}
	
	public int getNumberOfCommentsByLogin(String login) {
		int numberOfCommentsByLogin = 0;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();)
			if(it.next().getLogin().equals(login))
				numberOfCommentsByLogin++;
		return numberOfCommentsByLogin;
	}
	
	public void save(){
		DBFacade.getInstance().saveComments(comments);
	}
	
	
	private String getNewId(){
		return String.valueOf(DBFacade.getInstance().getLastCommentId());
	}
	
	private CommentBean getCommentFromId(String id){
		CommentBean comment;
		for(Iterator<CommentBean> it = comments.iterator(); it.hasNext();){
			comment = it.next();
			if(comment.getId().equals(id))
				return comment;
		}
		return null;
	}
}
